package user

import (
	"context"
	"errors"
	"strconv"

	"hzjt-oam/admin-api/internal/svc"
	"hzjt-oam/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserInfoLogic {
	return &UserInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserInfoLogic) UserInfo(req *types.UserInfoReq) (resp *types.UserInfoResp, err error) {
	atoi, err := strconv.Atoi(req.ID)
	if err != nil {
		return nil, errors.New("id类型不合法")
	}
	one, err := l.svcCtx.UserModel.FindOne(l.ctx, int64(atoi))
	if err != nil {
		return nil, errors.New("用户获取失败")
	}
	dept_one, _ := l.svcCtx.UserDeptModel.FindOne(l.ctx, one.DeptId)
	role_one, _ := l.svcCtx.UserRoleModel.FindOne(l.ctx, one.RoleId)

	return &types.UserInfoResp{
		Avatar:         one.Avatar,
		Name:           one.Name,
		Age:            int8(one.Age.Int64),
		Sex:            one.Sex.String,
		NickName:       one.NickName,
		Email:          one.Email,
		Mobile:         one.Mobile,
		Status:         one.Status,
		DeptName:       dept_one.Name.String,
		CreateBy:       one.CreateBy,
		CreateTime:     one.CreateTime,
		LastUpdateBy:   one.LastUpdateBy,
		LastUpdateTime: one.LastUpdateTime,
		DelFlag:        one.DelFlag,
		RoleName:       role_one.Name.String,
	}, err
}
